package com.ruoyi.merchant.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.db.domain.SysDeptCopy;
import com.ruoyi.db.mapper.SysDeptCopyMapper;
import com.ruoyi.merchant.constant.MerchantConstant;
import com.ruoyi.merchant.service.ISysDeptCopyService;
import org.springframework.stereotype.Service;

@Service
public class SysDeptCopyServiceImpl extends ServiceImpl<SysDeptCopyMapper, SysDeptCopy> implements ISysDeptCopyService {

    @Override
    public SysDeptCopy saveInfo(SysDeptCopy sysDept){
        SysDeptCopy info = this.baseMapper.selectById(sysDept.getParentId());
        // 如果父节点不为正常状态,则不允许新增子节点
        if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
        {
            throw new ServiceException("部门停用，不允许新增");
        }
        sysDept.setAncestors(info.getAncestors() + "," + sysDept.getParentId());
        int insert = this.baseMapper.insert(sysDept);
        if (insert > 0){
            return sysDept;
        }
        return null;
    }



}
